package com.wangyadong.hobby.thread;

import java.util.Random;
import java.util.stream.IntStream;

public class JavaStarvation implements Runnable {


    @Override
    public void run() {
        Random random = new Random(100000);
//        random.setSeed(System.nanoTime());
        random.nextInt(20000);
        while (true) {
            System.out.println(Thread.currentThread().getName() + "  access the critical section");
            if (random.nextInt() == 199) {
                return;
            }
        }
    }

    public static void main(String[] args) {
        IntStream.range(0, 10).forEach(i -> {
            Thread thread = new Thread(new JavaStarvation());
            if (i == 0 || i == 1) {
                thread.setPriority(Thread.MAX_PRIORITY);
                thread.setName(thread.getName() + " MAX_PRIORITY");
            } else {
                thread.setPriority(Thread.MIN_PRIORITY);
                thread.setName(thread.getName() + " MIN_PRIORITY");
            }

            thread.start();
        });


    }
}
